﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Dapper.Sharding.Maui
{
	public class LockManagerAsync
	{
		private readonly object _lock = new object();

		private readonly Dictionary<string, SemaphoreSlim> dict = new Dictionary<string, SemaphoreSlim>();

		public SemaphoreSlim GetObjct(string name)
		{
			var exists = dict.TryGetValue(name, out var val);
			if (!exists)
			{
				lock (_lock)
				{
					if (!dict.ContainsKey(name))
					{
						dict.Add(name, new SemaphoreSlim(1, 1));
					}
				}
				val = dict[name];
			}
			return val;
		}
	}
}
